package com.kh.first.controller;

import com.kh.first.pojo.common.AjaxJson;
import ${beanAllName};
import com.kh.first.pojo.common.LayTableJson;
import com.kh.first.pojo.common.PageParam;

import ${serviceAllName};
import com.kh.first.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("${beanName}Controller")
public class UserTestController {
    static final String currentFolder = UserUtils.THEME + "${beanName?lower_case}/";
    @Autowired
    private ${serviceClassName} ${serviceClassName?uncap_first};

    @RequestMapping(params = "go${beanName}List")
    public String go${beanName}List() {
    return currentFolder + "${beanName?lower_case}-list";
    }

    <#--@RequestMapping(params = "home")-->
    <#--public String home() {-->
        <#--return UserUtils.THEME + "home";-->
    <#--}-->

    @RequestMapping(params = "dataGrid")
    @ResponseBody
    public LayTableJson<${beanClassName}> dataGrid(${beanClassName} ${beanPostfix?lower_case}, PageParam pageParam) {
        LayTableJson<${beanClassName}> ajaxJson = new LayTableJson<>();
        int limit = pageParam.getLimit();
        int page = pageParam.getPage();
        ${beanPostfix?lower_case}.setStart(page == 0 ? 1 : (page - 1) * limit);
        ${beanPostfix?lower_case}.setSize(limit == 0 ? 10 : limit);
        List<${beanClassName}> ${beanName}List = ${serviceClassName?uncap_first}.get${beanName}List(${beanPostfix?lower_case});
        ajaxJson.data = ${beanName}List;
        return ajaxJson;
    }

    @RequestMapping(params = "go${beanName}Add")
    public String go${beanName}Add() {
        return currentFolder + "${beanName}-add-update";
    }

    @RequestMapping(params = "go${beanName}Query")
    public String go${beanName}Query(Model model,${beanClassName} ${beanPostfix?lower_case}){
        model.addAttribute("${beanName?lower_case}", ${beanPostfix?lower_case});
        return currentFolder +"${beanName?lower_case}-query";
    }

    @RequestMapping(params = "go${beanName}Update")
    public String go${beanName}Update(${beanClassName} ${beanName?lower_case}, Model model) {
        List<${beanClassName}> ${beanName}List = ${serviceClassName?uncap_first}.get${beanName}List(${beanName?lower_case});
        ${beanName?lower_case} = ${beanName}List.get(0);//报错让他报
        model.addAttribute("${beanName?lower_case}",${beanName?lower_case});
        return currentFolder + "${beanName?lower_case}-add-update";
    }

    @RequestMapping(params = "do${beanName}Add")
    @ResponseBody
    public AjaxJson<String> doUserAdd(${beanClassName} ${beanName?lower_case}) {
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        ${serviceClassName?uncap_first}.save${beanName}(${beanName?lower_case});
        return ajaxJson;
    }
    @RequestMapping(params = "do${beanName}Update")
    @ResponseBody
    public AjaxJson<String> do${beanName}Update(${beanClassName} ${beanName?lower_case}) {
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        ${serviceClassName?uncap_first}.update${beanName}Id(${beanName?lower_case});
        return ajaxJson;
    }

}
